package com.lyl.momanapp.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lyl.momanapp.R;
import com.lyl.momanapp.bean.URL;
import com.lyl.momanapp.bean.User;
import com.lyl.momanapp.util.utlis.MyUtils;
import com.lyl.momanapp.util.view.MyBaseActivity;

import java.util.HashMap;

/**
 * 名字 梁亚龙
 * 时间 2017/9/26.
 * QQ邮箱 503934353@qq.com
 * 登陆页面
 */

public class UserLoginActivity extends MyBaseActivity {


    private TextView mRegisterTV;
    private static final int register = 0;
    private static final int reset = 1;
    private EditText mPwd;
    private EditText mPhone;
    private TextView mReset;
    private Button mLogin;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case URL.ID_USER_LOGIN: {
                    URL.Userlogin = new Gson().fromJson(msg.obj.toString(), User.login.class);
                    Log.i("111", "handleMessage: " + msg.obj.toString());
                    if (URL.Userlogin.getState().equals("M00000")) {
                        Toast.makeText(UserLoginActivity.this, URL.Userlogin.getMessage(), Toast.LENGTH_SHORT).show();
                        UserLoginActivity.this.finish();
                    } else {
                        Toast.makeText(UserLoginActivity.this, URL.Userlogin.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                }
                break;
            }
        }
    };

    @Override
    public void initUI() {
        setContentView(R.layout.activity_login);
        mRegisterTV = ((TextView) findViewById(R.id.user_register_tv));
        mReset = ((TextView) findViewById(R.id.user_reset));
        mPwd = ((EditText) findViewById(R.id.user_pwd));
        mPhone = ((EditText) findViewById(R.id.user_phone));
        mLogin = ((Button) findViewById(R.id.login_but));
        if (URL.Userlogin != null) {
            finish();
        }
    }

    @Override
    public void initData() {

    }

    @Override
    public void addListeners() {
        mRegisterTV.setOnClickListener(this);
        mReset.setOnClickListener(this);
        mLogin.setOnClickListener(this);
        findViewById(R.id.fanhui).setOnClickListener(this);
        findViewById(R.id.zhuye).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.user_register_tv: {
                Intent intent = new Intent(UserLoginActivity.this, UserRegisterActivity.class);
                startActivity(intent);
            }
            break;
            case R.id.user_reset: {
                Intent intent = new Intent(UserLoginActivity.this, UserResetActivity.class);
                startActivityForResult(intent, reset);
            }

            break;
            case R.id.login_but: {

                HashMap<String, String> map = new HashMap<>();
                map.put("phone", mPhone.getText().toString().trim());
                map.put("access_type", 1 + "");
                map.put("password", mPwd.getText().toString().trim());
                new MyUtils().xUtilsPost(URL.URL_USER_LOGIN, map, handler, URL.ID_USER_LOGIN);

            }
            break;
            case R.id.fanhui: {
                finish();
            }
            break;
            case R.id.zhuye: {

                startActivity(new Intent(UserLoginActivity.this, MainActivity.class));
                finish();
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//        switch (requestCode) {
//            case register: {
//                Bundle MarsBuddle = data.getExtras();
//                String name = MarsBuddle.getString("name");
//                String pwd = MarsBuddle.getString("pwd");
//                mPhone.setText(name);
//                mPwd.setText(pwd);
//            }
//            break;
//            case reset: {
//                Bundle MarsBuddle = data.getExtras();
//                String name = MarsBuddle.getString("name");
//                String pwd = MarsBuddle.getString("pwd");
//                mPhone.setText(name);
//                mPwd.setText(pwd);
//            }
//            break;

//        }
    }
}
